package com.first.agent.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.first.agent.common.R;
import com.first.agent.pojo.AgentCustomer;
import com.first.agent.pojo.Contacts;
import com.first.agent.service.CustomerService;
import com.first.agent.service.impl.CustomerServiceImp;
import com.first.agent.utils.DateUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created with IntelliJ IDEA.
 * User: lxm
 * Date: 2022/11/16 16:09
 * Description:
 * Version: V1.0
 */
@WebServlet("/customer/update")
public class EditCustomerServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取客户端提交的参数
        String cus_id = request.getParameter("cus_id");
        String company_name = request.getParameter("company_name");
        String company_home = request.getParameter("company_home");
        String company_type = request.getParameter("company_type");
        String state = request.getParameter("state");
        String represent = request.getParameter("represent");
        String registration_time = request.getParameter("registration_time");
        String document_type = request.getParameter("document_type");
        String document_num = request.getParameter("document_num");
        String country = request.getParameter("country");
        String provinces = request.getParameter("provinces");
        String city = request.getParameter("city");
        String area = request.getParameter("area");
        String company_fax = request.getParameter("company_fax");
        String company_tel = request.getParameter("company_tel");
        String note = request.getParameter("note");
        String company_address = request.getParameter("company_address");

        //联系人
        String con_id = request.getParameter("con_id");
        String con_name = request.getParameter("con_name");
        String con_tel = request.getParameter("con_tel");
        String con_fax = request.getParameter("con_fax");
        String con_email = request.getParameter("con_email");
        String con_duties = request.getParameter("con_duties");


        //讲数据存入到对象中，方便参数传递
        AgentCustomer customer = new AgentCustomer();
        Contacts contacts=new Contacts();

        customer.setCus_id(Integer.parseInt(cus_id));
        customer.setCompany_name(company_name);
        customer.setCompany_home(company_home);
        customer.setCompany_type(company_type);
        customer.setState(Integer.parseInt(state));
        customer.setRepresent(represent);
        customer.setRegistration_time(DateUtils.string2Date(registration_time));
        customer.setCompany_type(company_type);
        customer.setDocument_type(document_type);
        customer.setDocument_num(document_num);
        customer.setCountry(country);
        customer.setProvinces(provinces);
        customer.setCity(city);
        customer.setArea(area);
        customer.setCompany_fax(company_fax);
        customer.setCompany_tel(company_tel);
        customer.setCompany_address(company_address);
        customer.setNote(note);

//        contacts.setCon_id();
        contacts.setCon_name(con_name);
        contacts.setCon_tel(con_tel);
        contacts.setCon_fax(con_fax);
        contacts.setCon_email(con_email);
        contacts.setCon_duties(con_duties);

        //调用service
        CustomerService csi=new CustomerServiceImp();
        R r = csi.updateCustomer(customer, contacts);
        //响应
        response.setContentType("application/json;charset=utf-8");
        PrintWriter pw = response.getWriter();
        pw.write(JSONObject.toJSONString(r));
        pw.flush();
        pw.close();
    }
}
